package com.sitech.openapi.controller;

import com.sitech.openapi.componet.SetHttpHeaderCmpt;
import com.sitech.openapi.domain.*;
import com.sitech.openapi.service.QryCustInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.NativeWebRequest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "获取客户的相关信息",description = "获取客户相关信息的控制层")
@RestController
public class QryCustInfoController {

    @Autowired
    QryCustInfoService qryCustInfoService;
    @Autowired
    private SetHttpHeaderCmpt setHttpHeaderCmpt;

    @GetMapping("/eCCustomer")
    @ApiOperation(value = "查询客户信息",notes = "客户信息API")
    public ResponseEntity<ECCustomer> eCCustomer(@RequestParam String serialNumber,
                                                 @RequestParam String custType,
                                                 @RequestParam String lanId,
                                                 @RequestHeader HttpHeaders headers){
        ECCustomer ecCustomer = qryCustInfoService.getECCustomerInfo(serialNumber,custType,lanId);
        HttpHeaders httpHeaders = setHttpHeaderCmpt.setOKHttpHeaders(headers);
        ResponseEntity<ECCustomer> resp = new ResponseEntity<ECCustomer>(ecCustomer,httpHeaders, HttpStatus.OK);
        return  resp;
    }

    @GetMapping("/eCAccount")
    @ApiOperation(value = "查询账户信息",notes = "账户信息API")
    public ResponseEntity<ECAccount> eCAccount(@RequestParam String serialNumber,
                                                 @RequestParam String lanId,
                                                 @RequestHeader HttpHeaders headers) throws Exception{

        ECAccount ecAccount = qryCustInfoService.getECAccountInfo(serialNumber,lanId);
        HttpHeaders httpHeaders = setHttpHeaderCmpt.setOKHttpHeaders(headers);
        ResponseEntity<ECAccount> resp = new ResponseEntity<ECAccount>(ecAccount,httpHeaders, HttpStatus.OK);
        return  resp;
    }

    @GetMapping("/eCProduct")
    @ApiOperation(value = "用户信息查询",notes = "用户信息API")
    public ResponseEntity<List<ECProduct>> eCProduct(@RequestParam(required = false) String serialNumber,
                                               @RequestParam(required = false) String accountId,
                                               @RequestParam(required = false) String custId,
                                               @RequestParam String lanId,
                                               @RequestHeader(required = false) HttpHeaders headers){
        List<ECProduct> ecProductList = qryCustInfoService.getECProductInfos(serialNumber,accountId,custId,lanId);
        HttpHeaders httpHeaders = setHttpHeaderCmpt.setOKHttpHeaders(headers);
        ResponseEntity<List<ECProduct>> resp = new ResponseEntity<List<ECProduct>>(ecProductList,httpHeaders, HttpStatus.OK);
        return  resp;
    }

    @ExceptionHandler
    public ResponseEntity<Map<String, String>> ContactTaskError(Exception e, NativeWebRequest request) {

        Map<String, String> errors = new HashMap<>();
        String requestId = request.getHeader("X-CTG-Request-ID");
        errors.put("code", HttpStatus.BAD_REQUEST.toString());
        errors.put("reason", e.getMessage());
        errors.put("message",e.toString());
        errors.put("referenceError","");
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.set("X-CTG-Request-ID",requestId);
        ResponseEntity<Map<String, String>> re = new ResponseEntity<>(errors, httpHeaders, HttpStatus.BAD_REQUEST);
        return re;
    }

}
